package com.hcb.micro.sdk;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;
import java.util.Random;

import javax.imageio.ImageIO;

import com.hcb.micro.util.UtilProperties;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyun.oss.model.ObjectMetadata;

public class OSSUPUtil {

	public static final String BUCKET_NAME = UtilProperties.getInstance("system.properties").getProperties("bucketName");
	public static final String ACCESS_KEY_ID = UtilProperties.getInstance("system.properties").getProperties("accessKeyId");
	public static final String ACCESS_KEY_SECRET = UtilProperties.getInstance("system.properties").getProperties("accessKeySecret");
	public static final String END_POINT = UtilProperties.getInstance("system.properties").getProperties("endPoint");
	public static final String DOMAIN_NAME = UtilProperties.getInstance("system.properties").getProperties("domainName");
	public static final String FOLDER_PATH = UtilProperties.getInstance("system.properties").getProperties("folderPath");


	// 在自己内部定义自己的一个 实例，只供内部调用
//	public static final OSSClient client = new OSSClient("http://oss-cn-beijing.aliyuncs.com", "LTAIEBYdbuuY3HCI", "2U6js4HyZzNV7BUB01nE2hEyx5ACh1");
	public static final OSSClient client = new OSSClient(END_POINT, ACCESS_KEY_ID,ACCESS_KEY_SECRET);

	private static File createSampleFile() throws IOException {
		File file = File.createTempFile("oss-java-sdk-", ".txt");
		file.deleteOnExit();

		Writer writer = new OutputStreamWriter(new FileOutputStream(file));
		writer.write("abcdefghijklmnopqrstuvwxyz\n");
		writer.write("0123456789011234567890\n");
		writer.close();

		return file;
	}

	private static void displayTextInputStream(InputStream input) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(input));
		while (true) {
			String line = reader.readLine();
			if (line == null)
				break;

			System.out.println("\t" + line);
		}
		System.out.println();

		reader.close();
	}

	// 上传文件 file
	public static void uploadIMG(String key, MultipartFile file) throws OSSException, ClientException, IOException {
		// 创建上传Object的Metadata
		InputStream is = file.getInputStream();//按此可以得到流 
		ObjectMetadata meta = new ObjectMetadata();
		meta.setContentType("image/*");
		OSSUPUtil.client.putObject("bucket20160601", key, is, meta);
	}
	
	// 上传文件 file
	public static void uploadFile(String key, File file) throws OSSException, ClientException, FileNotFoundException {
		// 获取指定文件的输入流
		InputStream content = new FileInputStream(file);
		// 创建上传Object的Metadata
		ObjectMetadata meta = new ObjectMetadata();
		// 必须设置ContentLength
		meta.setContentLength(file.length());
		meta.setContentType("image/*");
		client.putObject("feichs", key, content, meta);
	}
	
	public static void uploadIMG(String key, BufferedImage bufferedImage) throws OSSException, ClientException, IOException {
		ByteArrayOutputStream os = new ByteArrayOutputStream();  
		ImageIO.write(bufferedImage, "jpg", os);  
		InputStream content = new ByteArrayInputStream(os.toByteArray());  
		client.putObject("feichs", key, content);
	}
	
	public static void uploadFile(String key, String fileUrl) throws OSSException, ClientException, IOException {
		InputStream inputStream = new FileInputStream(fileUrl);
		client.putObject("feichs", key, inputStream);
		client.shutdown();
	}
	/**
	 * 上传本地文件
	 * @param key
	 * @param fileUrl
	 * @throws OSSException
	 * @throws ClientException
	 * @throws IOException
	 */
	public static void uploadLocalFile(String key, String fileUrl) throws OSSException, ClientException, IOException {
		client.putObject("feichs", key, new File(fileUrl));
		client.shutdown();
	}
	
	/**
     * @Description: 根据key获取oss服务器上的图片地址
     * @param key
     * @return 
     * @ReturnType:String
    */
    public static String getImgURl(String key){
        GeneratePresignedUrlRequest generatePresignedUrlRequest =new GeneratePresignedUrlRequest("feichs", key);
        URL url = client.generatePresignedUrl(generatePresignedUrlRequest);
        return url.toString();
    }
}
